package com.aglhz.nature.presenter;

import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;

import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.SecondClassificationViewEvent;
import com.aglhz.nature.modle.SecondClassificationImage;
import com.aglhz.nature.modle.item.classification.SecondClassificationListData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.SecondClassificationView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class SecondClassificationPresenter extends BasePresenter {
	private static String TAG = SecondClassificationPresenter.class.getName();

	// view
	private SecondClassificationView secondclassificationView;
	// data
	private SecondClassificationImage secondclassificationImage;
	private List<SecondClassificationListData> secondClassificationListDatas;

	public SecondClassificationPresenter(SecondClassificationView view) {
		super();
		secondclassificationView = view;

	}

	// /*********************data operation*******************///
	public void loadData() {
		// load remote data
		String urlId = secondclassificationView.setUrlId();
		Log.e("mcp--------------------------", urlId);
		this.loadListImage(urlId);

		// save remote data

	}

	// /*********************view operation*******************///
	public void showListView() {
		// get data array list
if(0==secondclassificationImage.getData().size()){
	secondclassificationView.showErrorView();
}else{
	

		secondclassificationView
				.setAdapter(secondclassificationImage.getData());

		Log.e("mcp--------------------------", "setadapter");

	}
	}
	// /********************view operation*******************///
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventSecondClassificationThread(
			SecondClassificationViewEvent event) {

		String msg = event.getMessage();
		Log.i(TAG, "SecondClassificationThread:" + msg);
		// update UI By event
		if (SecondClassificationViewEvent.EVENT_UPDATE_SECONDCLASSIFICATION_LISTVIEW == msg) {
			// update grid view
			Log.d(TAG, "secondclassificationImage---yyyyyyyyyyyyy---"
					+ secondclassificationImage.getData());

			this.showListView();

			Log.d(TAG, "secondclassificationImage---gggggggggggggggg---"
					+ secondclassificationImage.getData());
		}

	}

	// /*********************private method*******************///
	private void loadListImage(String id) {

		// String urlId=secondclassificationView.setUrlId(id);

		// http://h5.aglhz.com/gc_community/member/goodscategory/subCategoryLevelList.do?232b7cb6-efd3-40b8-bf9c-f69cfc0b368f&page=0&pageSize=10
		
		String url = ServerAPI.APIHEARD+ServerAPI.SECOMD_CLASSIFICATION+"?id="+id+"&page=0&pageSize=10";
		Log.e("mcpmcpmcmcccc--------------urlid------------", url);

		AsyncHttpClient client = new AsyncHttpClient();
		client.get(url, new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				try {
					Log.e(TAG, "response" + responseString);

					Gson gson = new Gson();

					SecondClassificationImage secondClassificationBean = gson
							.fromJson(responseString,
									SecondClassificationImage.class);
					secondclassificationImage = (SecondClassificationImage) secondClassificationBean
							.clone();

					Log.d(TAG, "secondclassificationImage---mmm-----"
							+ secondclassificationImage);
					Log.d(TAG, "secondclassificationImage---111-----"
							+ secondclassificationImage.getOther());
					Log.d(TAG, "secondclassificationImage---eeeee---"
							+ secondclassificationImage.getData());

					secondClassificationListDatas = secondclassificationImage
							.getData();

					Log.d(TAG, "secondClassificationListData---mcp-----"
							+ secondClassificationListDatas);
					// send event
					SecondClassificationViewEvent event = new SecondClassificationViewEvent(
							SecondClassificationViewEvent.EVENT_UPDATE_SECONDCLASSIFICATION_LISTVIEW);
					EventBus.getDefault().post(event);

					Log.e(TAG, "success----------------------------"
							+ "success");
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {

				Log.e(TAG, "failure");
			}

		});

	}
}
